package com.spring.shopping.service.impl;

import com.spring.shopping.entity.Cart;
import com.spring.shopping.entity.CartItem;
import com.spring.shopping.entity.Product;
import com.spring.shopping.mapper.CartMapper;
import com.spring.shopping.mapper.ProductMapper;
import com.spring.shopping.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;


/*
 * @Coding By ZTK:
 */


@Service
@Transactional
public class CartServiceImpl implements CartService {

    @Autowired
    private CartMapper cartMapper;

    @Autowired
    private ProductMapper productMapper;

    @Override
    public List<CartItem> getCartItemsByIds(Integer userId, List<Integer> cartIds) {
        List<CartItem> items = cartMapper.selectCartItemsByIds(userId, cartIds);

        for (CartItem item : items) {
            BigDecimal subtotal = BigDecimal.valueOf(item.getProduct().getPrice()).multiply(BigDecimal.valueOf(item.getQuantity()));

            item.setSubtotal(subtotal);
        }

        return items;
    }

    @Override
    public void addToCart(Integer userId, Integer productId, Integer quantity) {
        Cart existingItem = cartMapper.selectCartItemByUserAndProduct(userId, productId);
        if (existingItem != null) {
            cartMapper.updateCartItemQuantity(existingItem.getId(), existingItem.getQuantity() + quantity);
        } else {
            Cart cart = new Cart();
            cart.setUserId(userId);
            cart.setProductId(productId);
            cart.setQuantity(quantity);
            cartMapper.insertCartItem(cart);
        }
    }

    @Override
    public void updateCartItem(Integer cartId, Integer quantity) {
        if (quantity < 1) {
            throw new IllegalArgumentException("数量必须大于0");
        }
        cartMapper.updateCartItemQuantity(cartId, quantity);
    }

    @Override
    public void removeFromCart(Integer cartId) {
        cartMapper.deleteCartItem(cartId);
    }

    @Override
    public List<CartItem> getCartItems(Integer userId) {

        List<Cart> cartList = cartMapper.selectCartItemsByUserId(userId);
        List<CartItem> cartItems = new ArrayList<>();

        for (Cart cart : cartList) {

            Product product = productMapper.selectProductById(cart.getProductId());
            if (product != null) {
                CartItem item = new CartItem();
                item.setCartId(cart.getId());
                item.setProduct(product); // 关键：设置完整的商品对象
                item.setQuantity(cart.getQuantity());
                cartItems.add(item);
            } else {

                System.out.println("shopping none"+cart.getProductId());
            }
        }
        return cartItems;
    }

    @Override
    public Double getCartTotal(Integer userId) {
        List<CartItem> cartItems = getCartItems(userId);
        double total = 0.0;

        for (CartItem item : cartItems) {

            if (item.getProduct() != null && item.getProduct().getPrice() != null) {
                total += item.getProduct().getPrice() * item.getQuantity();
            } else {

                System.out.println("shopping item error"+ item.getCartId());
            }
        }

        return Math.max(total, 0.0);
    }

    @Override
    public int getCartItemCount(Integer userId) {
        return cartMapper.selectCartItemCount(userId);
    }
}